3 * Cleanup all spam from a given hostname.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once __DIR__
. '/Maintenance.php';
27 * Maintenance script to cleanup all spam from a given hostname.
29 * @ingroup Maintenance
31 class CleanupSpam
extends Maintenance
{
33 public function __construct() {
34 parent
::__construct();
35 $this->addDescription( 'Cleanup all spam from a given hostname' );
36 $this->addOption( 'all', 'Check all wikis in $wgLocalDatabases' );
37 $this->addOption( 'delete', 'Delete pages containing only spam instead of blanking them' );
40 'Hostname that was spamming, single * wildcard in the beginning allowed'
44 public function execute() {
45 global $IP, $wgLocalDatabases, $wgUser;
47 $username = wfMessage( 'spambot_username' )->text();
48 $wgUser = User
::newSystemUser( $username );
50 $this->fatalError( "Invalid username specified in 'spambot_username' message: $username" );
52 // Hack: Grant bot rights so we don't flood RecentChanges
53 $wgUser->addGroup( 'bot' );
55 $spec = $this->getArg();
58 foreach ( [ 'http://', 'https://' ] as $prot ) {
59 $like = LinkFilter
::makeLikeArray( $spec, $prot );
61 $this->fatalError( "Not a valid hostname specification: $spec" );
63 $likes[$prot] = $like;
66 if ( $this->hasOption( 'all' ) ) {
67 // Clean up spam on all wikis
68 $this->output( "Finding spam on " . count( $wgLocalDatabases ) . " wikis\n" );
70 foreach ( $wgLocalDatabases as $wikiID ) {
71 /** @var $dbr Database */
72 $dbr = $this->getDB( DB_REPLICA
, [], $wikiID );
74 foreach ( $likes as $like ) {
75 $count = $dbr->selectField(
78 [ 'el_index' . $dbr->buildLike( $like ) ],
83 $cmd = wfShellWikiCmd(
84 "$IP/maintenance/cleanupSpam.php",
85 [ '--wiki', $wikiID, $spec ]
87 passthru( "$cmd | sed 's/^/$wikiID: /'" );
92 $this->output( "All done\n" );
94 $this->output( "None found\n" );
97 // Clean up spam on this wiki
100 /** @var $dbr Database */
101 $dbr = $this->getDB( DB_REPLICA
);
102 foreach ( $likes as $prot => $like ) {
105 [ 'DISTINCT el_from' ],
106 [ 'el_index' . $dbr->buildLike( $like ) ],
109 $count = $dbr->numRows( $res );
110 $this->output( "Found $count articles containing $spec\n" );
111 foreach ( $res as $row ) {
112 $this->cleanupArticle( $row->el_from
, $spec, $prot );
116 $this->output( "Done\n" );
123 * @param string $domain
124 * @param string $protocol
125 * @throws MWException
127 private function cleanupArticle( $id, $domain, $protocol ) {
128 $title = Title
::newFromID( $id );
130 $this->error( "Internal error: no page for ID $id" );
135 $this->output( $title->getPrefixedDBkey() . " ..." );
136 $rev = Revision
::newFromTitle( $title );
137 $currentRevId = $rev->getId();
139 while ( $rev && ( $rev->isDeleted( Revision
::DELETED_TEXT
)
140 || LinkFilter
::matchEntry( $rev->getContent( Revision
::RAW
), $domain, $protocol ) )
142 $rev = $rev->getPrevious();
145 if ( $rev && $rev->getId() == $currentRevId ) {
146 // The regex didn't match the current article text
147 // This happens e.g. when a link comes from a template rather than the page itself
148 $this->output( "False match\n" );
150 $dbw = $this->getDB( DB_MASTER
);
151 $this->beginTransaction( $dbw, __METHOD__
);
152 $page = WikiPage
::factory( $title );
154 // Revert to this revision
155 $content = $rev->getContent( Revision
::RAW
);
157 $this->output( "reverting\n" );
158 $page->doEditContent(
160 wfMessage( 'spam_reverting', $domain )->inContentLanguage()->text(),
161 EDIT_UPDATE | EDIT_FORCE_BOT
,
164 } elseif ( $this->hasOption( 'delete' ) ) {
165 // Didn't find a non-spammy revision, blank the page
166 $this->output( "deleting\n" );
167 $page->doDeleteArticle(
168 wfMessage( 'spam_deleting', $domain )->inContentLanguage()->text()
171 // Didn't find a non-spammy revision, blank the page
172 $handler = ContentHandler
::getForTitle( $title );
173 $content = $handler->makeEmptyContent();
175 $this->output( "blanking\n" );
176 $page->doEditContent(
178 wfMessage( 'spam_blanking', $domain )->inContentLanguage()->text(),
179 EDIT_UPDATE | EDIT_FORCE_BOT
182 $this->commitTransaction( $dbw, __METHOD__
);
187 $maintClass = CleanupSpam
::class;
188 require_once RUN_MAINTENANCE_IF_MAIN
;